package com.such.kit.validate.validator.impl;

import java.util.List;
import com.such.kit.datacast.CastCommons;
import com.such.kit.string.StringCommons;
import com.such.kit.validate.validator.Validator;

/**
 * @author SUCH
 * <pre>
 * 数据长度校验器
 * 
 * [参数集说明]
 * 参数集长度等于 1 时，输入内容长度必须大于 #[1]
 * 参数集长度等于 2 时，输入内容长度必须介于 #[1] 到 #[2] 之间
 * </pre>
 */
public class LengthValidator extends Validator {

	@Override
	protected boolean checkParams(List<String> params) {
		int paramsSize = params.size();
		if (paramsSize == 1 || paramsSize == 2) {
			return true;
		}
		return false;
	}

	@Override
	protected boolean verify(String value, List<String> params) {
		int paramsSize = params.size();
		int min = CastCommons.stringToInteger(params.get(0));
		int len = StringCommons.length(value, false);
		if (paramsSize == 1) {
			if (len >= min) {
				return true;
			}
		} else if (paramsSize == 2) {
			int max = CastCommons.stringToInteger(params.get(1));
			if (len >= min && len <= max) {
				return true;
			}
		}
		return false;
	}

	@Override
	protected String getVerifyMessage(List<String> params) {
		int paramsSize = params.size();
		String message = "";
		if (paramsSize == 1) {
			message = "输入内容长度必须大于 #[1]";
		} else if (paramsSize == 2) {
			if (params.get(0).equals(params.get(1))) {
				message = "输入内容长度必须等于 #[1]";
			} else {
				message = "输入内容长度必须介于 #[1] 到 #[2] 之间";
			}
		}
		return message;
	}

}
